package com.wx.disparent.mallparent.controller;

import com.wx.disparent.mallparent.entity.ProductEntity;
import com.wx.disparent.mallparent.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘杰龙
 * @version 1.0
 * @description
 * @date 2019/12/14 14:26
 */
@RestController
public class ProductControllerForPhone {
    @Autowired
    private ProductService productService;

    /**
     * 获取单个商品信息
     *
     * @param productId 商品ID
     * @param referId   推荐人ID
     * @return 商品信息
     */
    @RequestMapping("/front/product/getProductInfo")
    public ModelAndView getInfo(@RequestParam(value = "id") Integer productId,
                                @RequestParam(value = "referId", required = false) Integer referId) {
        ModelAndView model = new ModelAndView();
//        model.addObject("referId",referId);
        ProductEntity productEntity = productService.findById(productId);
        String entityImage = productEntity.getImage();

        List<Map<String,Object>> images = null;
        if (entityImage.indexOf(";") > 0) {
            images = new ArrayList();
            int index = 0;
            String[] image = entityImage.split(";");
            for (String s : image) {
                Map<String,Object> map = new HashMap<>();
                map.put("index",index);
                map.put("imgUrl",s);
                images.add(map);
                index++;
            }
        }
        model.addObject("images",images);
        model.addObject("product", productEntity);
        model.setViewName("web/detailPage");
        return model;
    }

    /**
     * 获取所有商品信息
     *
     * @return 商品集合
     */
    @RequestMapping("/")
    public ModelAndView frontIndex() {
        ModelAndView model = new ModelAndView();
        List<ProductEntity> entities = productService.findAllAvailable(null);
        model.addObject("products", entities);
//        if (entities.size() > 0) {
//            for (ProductEntity entity : entities) {
//                String entityImage = entity.getImage();
//                if (entityImage.indexOf(";") > 0) {
//                    String[] image = entityImage.split(";");
//                    entity.setImage(image[0]);
//                }
//            }
//        }
        model.setViewName("web/productList");
        return model;
    }

    /**
     * 获取所有商品信息
     *
     * @return 商品集合
     */
    @RequestMapping("/front/product/getAllProduct")
    public ModelAndView getAllInfo() {
        ModelAndView model = new ModelAndView();
        List<ProductEntity> entities = productService.findAllAvailable(null);
        model.addObject("products", entities);
        model.setViewName("web/productList");
        return model;
    }
}
